﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using Controller;
using Interface;

namespace Projekt3
{
    /// Projekt 3 - Hesehus
    /// Tien, Per, Mads & Mads
    /// zzz

    public partial class MainWindow : Window
    {
        MyController myController;

        //string til temp txt i søgefelt
        string _txt = "";

        public MainWindow()
        {
            InitializeComponent();

            // MyControler
            myController = new MyController();
            Initialize();
            //focus på normal søg felt som standard
            txtSearch.Focus();
        }

        public void Initialize()
        {
            // Update GUI
            /*
             * HUSK: Disse data skal sættes i MainWindow.xaml.cs og ikke i XAML-koden * * * 
             */
            Slider();

            // Vis indhold i combobox og gridResult, combobox for sort sørger for at hente ucresult
            CB();

            //ShowUCResult(cbSortBy.SelectedIndex);

            //forrig knap er inaktiv til at starte med
            btnPrev.IsEnabled = false;

            // Fjern label med teksten "Ingen produkter fundet"
            lblNoProducts.Visibility = Visibility.Hidden;
        }

        #region Initialize
        public void Slider()
        {
            lblMinValue.Content = myController.GetMinPrice();
            lblMaxValue.Content = myController.GetMaxPrice();

            // Sæt standard til slider
            PriceSlider.Value = (double)myController.GetMaxPrice();
            PriceSlider.Minimum = (double)myController.GetMinPrice();
            PriceSlider.Maximum = (double)myController.GetMaxPrice();
        }

        public void CB()
        {
            // Hent og læg kategorier ind i combobox
            cbCategory.ItemsSource = null;
            cbCategory.ItemsSource = myController.GetCategories();
            cbCategory.SelectedIndex = 0;

            // Hent og læg brands ind i combobox
            cbBrand.ItemsSource = null;
            cbBrand.ItemsSource = myController.GetBrands();
            cbBrand.SelectedIndex = 0;

            // Combobox mulighed for at sortere i resultater
            List<string> sortBy = myController.GetProductSortBy();
            foreach (string s in sortBy)
                cbSortBy.Items.Add(s);
            cbSortBy.SelectedIndex = 0;

            //pages
            Paging();
            //int pages = myController.GetMaxPageNumber();
            //for (int i = 1; i <= pages; i++)
            //    cbGo2Page.Items.Add(i);
            //cbGo2Page.SelectedIndex = 0;
        }
        #endregion

        #region UserControl
        public void ShowUCProduct(int id)
        {
            IProduct product = myController.FindProduct(id);
            UC.Product pg = new UC.Product(myController, product);
            gridResult.Children.Clear();
            gridResult.Children.Add(pg);
        }

        public void ShowUCResult(int pageNumber = 0)
        {
            Result rg = new Result(myController, pageNumber);
            gridResult.Children.Clear();
            gridResult.Children.Add(rg);
        }
        #endregion

        #region Events
        #region Button
        // Alm søgning submit
        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            string query = txtSearch.Text;
            if (myController.Search(query) == 1)
            {
                btnPrev.IsEnabled = false;
                btnNext.IsEnabled = false;
                cbGo2Page.Items.Clear();
                cbGo2Page.Items.Add(1);
                cbGo2Page.SelectedIndex = 0;
                ShowUCProduct(myController.GetProducts()[0].Id);
                
            }
            else
            {
                if (myController.Search(query) == 0)
                    lblNoProducts.Visibility = Visibility.Visible;
                else
                    lblNoProducts.Visibility = Visibility.Hidden;
                ShowUCResult();
                cbGo2Page.Items.Clear();
                Paging();
            }
        }

        // Live search
        private void txtSearch_TextChanged(object sender, TextChangedEventArgs e)
        {
            
        }

        // Adv søgning submit
        private void btnAdvSearch_Click(object sender, RoutedEventArgs e)
        {
            // Søge kriterier
            string query = txtAdvSearch.Text;
            decimal maxPrice = decimal.Parse(PriceSlider.Value.ToString());
            ICategory category = (ICategory)cbCategory.SelectedItem;
            IBrand brand = (IBrand)cbBrand.SelectedItem;

            int result = myController.AdvSearch(query, maxPrice, category, brand);

            if (result == 1)
            {
                btnPrev.IsEnabled = false;
                btnNext.IsEnabled = false;
                cbGo2Page.Items.Clear();
                cbGo2Page.Items.Add(1);
                cbGo2Page.SelectedIndex = 0;
                ShowUCProduct(myController.GetProducts()[0].Id);
            }
            else
            {
                if (result == 0)
                    lblNoProducts.Visibility = Visibility.Visible;
                else
                    lblNoProducts.Visibility = Visibility.Hidden;
                ShowUCResult();
                cbGo2Page.Items.Clear();
                Paging();
            }
        }

        // Tilbage knappen
        // Vis Alle fucking knap
        private void btnShowAll_Click(object sender, RoutedEventArgs e)
        {
            myController.Search("");
            cbGo2Page.Items.Clear();
            Paging();
            ShowUCResult();
        }
        #endregion

        #region TextBox Focus
        private void TextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            TextBox tempTxt = (TextBox)e.Source;
            _txt = tempTxt.Text;
            if (tempTxt.Text == "Søgeord")
                tempTxt.Clear();

            if (tempTxt == txtAdvSearch)
            {
                btnSearch.IsDefault = false;
                btnAdvSearch.IsDefault = true;
            }
            else if (tempTxt == txtSearch)
            {
                btnSearch.IsDefault = true;
                btnAdvSearch.IsDefault = false;
            }
        }

        private void TextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            TextBox tempTxt = (TextBox)e.Source;

            if (tempTxt.Text == "" || tempTxt.Text.Length == 0)
            {
                if (tempTxt == txtSearch || tempTxt == txtAdvSearch)
                    tempTxt.Text = "Søgeord";
                else
                    tempTxt.Text = _txt;
            }
        }
        #endregion

        // Pris slideren
        private void PriceSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            decimal priceValue = (decimal)(((Slider)sender).Value);
            lblMaxPrice.Content = priceValue.ToString("0") + " kr.";
            myController.CurrentPrice = priceValue;
        }

        #region Navigation
        public void Paging()
        {
            int pages = myController.GetMaxPageNumber();
            int lastPage = 0;
            if (cbGo2Page.Items.Count > 0)
                lastPage = cbGo2Page.Items.Count - 1;

            if (lastPage > 0)
            {
                if ((int)cbGo2Page.Items[lastPage] != pages)
                    cbGo2Page.Items.Clear();
            }
            for (int i = 1; i <= pages; i++)
                cbGo2Page.Items.Add(i);

            cbGo2Page.SelectedIndex = 0;
        }

        private void cbGo2Page_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int index = cbGo2Page.SelectedIndex;
            int maxPage = myController.GetMaxPageNumber();

            if (index == 0)
            {
                if (maxPage == 1)
                    btnNext.IsEnabled = false;
                else
                {
                    btnNext.IsEnabled = true;
                    btnPrev.IsEnabled = false;
                }
            }
            else if (index + 1 == maxPage) //last page
            {
                btnNext.IsEnabled = false;
                btnPrev.IsEnabled = true;
            }
            else if (index > 0)
            {
                btnPrev.IsEnabled = true;
            }
            ShowUCResult(index);
        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            int nextPage = cbGo2Page.SelectedIndex + 1;
            if (nextPage < myController.GetMaxPageNumber())
            {
                cbGo2Page.SelectedIndex = nextPage;
                ShowUCResult(nextPage);
            }
        }

        private void btnPrev_Click(object sender, RoutedEventArgs e)
        {
            int prevPage = cbGo2Page.SelectedIndex - 1;
            if (prevPage > -1)
            {
                cbGo2Page.SelectedIndex = prevPage;
                ShowUCResult(prevPage);
            }

            if (myController.GetMaxPageNumber() >= cbGo2Page.SelectedIndex)
            {
                btnNext.IsEnabled = true;
            }
        }

        private void btnBack_Click(object sender, RoutedEventArgs e)
        {
            if (myController.GetProducts().Count == 1)
            {
                ShowUCResult(0);
            }

            ShowUCResult(cbGo2Page.SelectedIndex);
            if (myController.GetMaxPageNumber() > 1)
            {
                cbGo2Page.Items.Clear();
                Paging();
                btnNext.IsEnabled = true;
            }

            if (cbGo2Page.SelectedIndex != 0)
            {
                btnPrev.IsEnabled = true;
            }
        }
        #endregion

        #region Sort
        private void cbSortBy_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbSortBy.SelectedIndex == 2)
            {
                MessageBox.Show("NB! Navnefeltet er tomt og kan derfor ikke sorteres efter i øjeblikket.");
            }
            Paging();
            myController.SortBy = cbSortBy.SelectedIndex;
            cbGo2Page.SelectedIndex = 0;
            ShowUCResult();
        }

        private void cbCategory_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
        }
        #endregion
        #endregion
    }
}